package com.yc.yyds.biz;

import com.yc.yyds.Bean.Comment;
import com.yc.yyds.dao.CommentsDao;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class CommentBiz {
    CommentsDao dao = new CommentsDao();

    //查询
    public List<Comment> queryOneComment(String movieid) throws BizException {
        if (movieid==null){
            throw new BizException("业务异常请稍后再试！");
        }
        List<Map<String, Object>> comments = dao.queryComment(movieid);
        System.out.println("所有评论数量："+comments.size());
        List<Comment> commentAll = new ArrayList<>();
        //查找一级评论
        List<Map<String, Object>> rootComments = comments.stream().filter(comment -> (Integer) comment.get("pid") ==0).collect(Collectors.toList());
        for (Map<String, Object> rootComment : rootComments) {
            Comment com = new Comment();
            com.setId((Integer) rootComment.get("id"));
            com.setUserid((Integer) rootComment.get("userid"));
            com.setMovieid((Integer) rootComment.get("movieid"));
            com.setContent((String) rootComment.get("content"));
            com.setImage((String) rootComment.get("image"));
            com.setPid((Integer) rootComment.get("pid"));
            com.setUsername((String) rootComment.get("username"));
            com.setTime((Timestamp) rootComment.get("creat_time"));
            com.setPname((String) rootComment.get("pname"));
            com.setDingjiid((Integer) rootComment.get("dingjiid"));
            //查子评论
            List<Map<String, Object>> chird = comments.stream().filter(comment -> (rootComment.get("id").equals(comment.get("dingjiid"))&& Integer.parseInt(comment.get("pid").toString())!=0)).collect(Collectors.toList());
            List<Comment> son = new ArrayList<>();
            for (Map<String, Object> ch : chird) {
                Comment c =new Comment();
                c.setId((Integer) ch.get("id"));
                c.setUserid((Integer) ch.get("userid"));
                c.setMovieid((Integer) ch.get("movieid"));
                c.setContent((String) ch.get("content"));
                c.setImage((String) ch.get("image"));
                c.setPid((Integer) ch.get("pid"));
                c.setUsername((String) ch.get("username"));
                c.setTime((Timestamp) ch.get("creat_time"));
                c.setPname((String) ch.get("pname"));
                c.setDingjiid((Integer) ch.get("dingjiid"));
                son.add(c);
            }
            com.setChirdren(son);
            commentAll.add(com);
        }

        return commentAll;
    }

    //保存
    public void saveComment(String userid,String movieid,String content,String pid,String dingjiid) throws BizException {
        if (userid.isEmpty()){
            throw new BizException("请登录后在评论");
        }
        if (content.equals("")){
            throw new BizException("评论内容不能为空");
        }
        try {
            dao.saveComment(userid, movieid, content, pid, dingjiid);
        }catch (Exception e){
            throw new BizException("业务异常,请联系管理员");
        }
    }
    public void delete(String id) throws BizException {
        try {
            dao.delete(id);
        }catch (Exception e){
            throw new BizException("业务异常,请联系管理员");
        }
    }

    //查看电影信息
    public Map<String, Object> query(String movieid) throws BizException {
       try {
           return dao.moviedesc(movieid);
       }catch (Exception e){
           throw new BizException(e.getMessage());
       }
    }
}
